<template>
  <el-dialog
    :close-on-click-modal="false"
    :before-close="handleBeforeClose"
    :visible="true"
    :title="title"
    width="60%">
    <el-form ref="form" :model="form" :rules="rules" size="small" :disabled="disabled"  label-width="100px">
      <el-table :data="tableData">
        <el-table-column prop="parameter" label="训练参数" />
        <el-table-column label="训练开始">
          <template #default="scope">
            <el-input v-model="scope.row.csq" />
          </template>
        </el-table-column>
        <el-table-column label="训练结束">
          <template #default="scope">
            <el-input v-model="scope.row.csh" />
          </template>
        </el-table-column>
      </el-table>
      <el-form-item label="主观症状">
        <el-select
          v-model="selectedFeels"
          placeholder="请选择"
          multiple
          filterable
          clearable
          style="width: 100%;">
          <el-option
            v-for="feel in feelList"
            :key="feel.name"
            :label="feel.name"
            :value="feel.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="其他症状">
        <el-input type="textarea" v-model="otherFeel" />
      </el-form-item>
      <el-form-item label="前庭功能神经反应分级">
        <el-select
          v-model="form.fyfj"
          placeholder="请选择"
          width="100%">
          <el-option
            v-for="[key, value] in sjfyLevels"
            :key="value"
            :label="value"
            :value="key" />
        </el-select>
      </el-form-item>
      <el-row>
        <el-col :span="6">
          <el-form-item label="评定结果">
            <el-select
              v-model="form.result"
              placeholder="请选择"
            >
              <el-option
                v-for="[key, value] in resultOptions"
                :key="value"
                :label="value"
                :value="key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="记录人" prop="createBy">
            <el-input v-model="form.createBy" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="记录日期" prop="createTime">
            <!-- <el-input v-model="form.createTime" disabled</el-input> -->
            <el-date-picker v-model="form.createTime" :picker-options="pickerOptions" type="createTime" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleCommit" :disabled="disabled">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {get, add, edit} from '@/api/trainSwglqtgn'
import { mapGetters, mapActions } from 'vuex'

import { SJFY_LEVELS, RATE_RESULTS } from '@/utils/constants'
import { form2Table, table2Form } from '../../../utils/table'

export default {
  name: 'SwglqtgnDialog',
  props: {
    trainId: {
      type: Number,
      default: -1,
      required: true
    },
    itemId: {
      type: Number,
      default: -1,
      required: true
    },
  },
  computed: {
    disabled() {
      return this.form.id != null && !this.checkPer(['admin']);
    },
    ...mapGetters('feel', ['feelList'])
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }]
      },
      tableData: [
        { parameter: '心率(次/分)', csq: "", csh: ""},
        { parameter: '血压(mmHg)', csq: "", csh: ""},
        { parameter: '呼吸(次/分)', csq: "", csh: ""}
      ],
      form: { id: null, trainId: null, csq: null, csh: null, feels: null, fyfj: null, result: null, date: null, createBy: null},
      // title: '训练 8',
      title: '三维滚轮前庭功能稳定性',
      selectedFeels: [],
      otherFeel: '',
      sjfyLevels: SJFY_LEVELS,
      resultOptions: RATE_RESULTS,
      isLoading: false
    }
  },
  methods: {
    ...mapActions('feel', ['getFeelList']),
    emit: function(event) {

    },
    handleBeforeClose(done) {
      this.$emit('close', this.itemId)
      if (done) {
        done(); // 调用done()来关闭对话框        
      }
    },
    handleCommit() {

      this.form.feels = this.selectedFeels.join('|') + '|' + this.otherFeel;
      table2Form(this.tableData, this.form, 'cs')
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.isLoading = true; // 提交按钮的加载状态
          this.form.trainId = this.trainId;
          this.form.trainItemId = this.itemId;
          if (this.form.id == null) {
            add(this.form).then(() => {
              this.$emit('close', this.itemId);
              this.isLoading = false;
            }).catch(() => {
              this.isLoading = false;
            });
          } else {
            edit(this.form).then(() => {
              this.$emit('close', this.itemId);
              this.isLoading = false;
            }).catch(() => {
              this.isLoading = false;
            });
          }
        }
      })
    },
    handleCancel() {
      this.$emit('cancel', this.itemId);
    }
  },
  created() {
    if (this.trainId !== -1) {
      // 使用 Vuex 获取感受列表
      this.getFeelList({current: 1, size: 100})
      this.shouldUpdate = false;

      get(this.trainId, this.itemId).then(response => {
        this.form = Object.assign(this.form, response);
        const feels = this.form.feels.split('|');
        if (feels.length > 1) {
          this.selectedFeels = feels.slice(0, -1).map(id => parseInt(id))
          this.otherFeel = feels[feels.length - 1]
        }
        form2Table(this.form, this.tableData, 'cs')
        console.log(response, this.form);

      });
    }
  }
}
</script>

<style scoped>
</style>